﻿Imports System.Xml.Serialization
Imports System.Xml

Namespace CMISObjects
   <XmlRoot("content", Namespace:=CmisNamespaceConstants.Cmisra)>
   Public Class ContentContainer
      Implements IXmlSerializable
      Public Sub New()

      End Sub
      Public Sub New(content As Byte(), mediaType As String)
         _base64 = Convert.ToBase64String(content)
         _mediaType = mediaType
      End Sub
      Public Sub New(base64 As String, mediaType As String)
         _base64 = base64
         _mediaType = mediaType
      End Sub
      Private _base64 As String
      Public ReadOnly Property Base64 As String
         Get
            Return _base64
         End Get
      End Property
      Public ReadOnly Property Content As Byte()
         Get
            If String.IsNullOrEmpty(_base64) Then
               Return Nothing
            End If
            Return Convert.FromBase64String(_base64)
         End Get
      End Property

      Private _mediaType As String
      Public ReadOnly Property MediaType As String
         Get
            Return _mediaType
         End Get
      End Property


      Public Function GetSchema() As System.Xml.Schema.XmlSchema Implements System.Xml.Serialization.IXmlSerializable.GetSchema
         Return Nothing
      End Function

      Public Sub ReadXml(reader As System.Xml.XmlReader) Implements System.Xml.Serialization.IXmlSerializable.ReadXml
         Dim doc As New System.Xml.XmlDocument()
         doc.Load(reader)
         Dim node As XmlNode = Common.GetItemCaseInsensitiveFromXML(doc, Common.GetXMLRootName(GetType(ContentContainer)))
         For Each child As XmlNode In node.ChildNodes
            Dim lname As String = Common.GetNodeNameWithoutPrefix(child).ToLowerInvariant
            If lname.Equals("mediatype") Then
               _mediaType = child.InnerText
            ElseIf lname.Equals("base64") Then
               _base64 = child.InnerText
            End If
         Next
      End Sub

      Public Sub WriteXml(writer As System.Xml.XmlWriter) Implements System.Xml.Serialization.IXmlSerializable.WriteXml
         writer.WriteElementString("mediatype", CmisNamespaceConstants.Cmisra, Me.MediaType)
         writer.WriteElementString("base64", CmisNamespaceConstants.Cmisra, Me.Base64)
      End Sub
   End Class
End Namespace